/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.web;

import com.google.common.collect.Sets;
import com.hyts.infodb.utils.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.ConfigurableApplicationContext;
import sun.net.util.IPAddressUtil;

import java.net.InetAddress;
import java.util.Set;

/**
 * @project-name:infodb
 * @package-name:com.hyts.infodb.web
 * @author:LiBo/Alex
 * @create-date:2020-03-06 22:05
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
@Slf4j
public class BootApplicationTemplate {


    /**
     * 操作服务
     * @param clazz
     * @param args
     */
    public static void boot(Class clazz,String[] args){
        log.info("--------------------------------------------------------------");
        log.info("开始启动项目......");
        SpringApplication springApplication = new SpringApplication();
        springApplication.setHeadless(true);
        springApplication.setLogStartupInfo(true);
        springApplication.setWebApplicationType(WebApplicationType.SERVLET);
        Set<String> sets = Sets.newHashSet(clazz.getName());
        springApplication.setSources(sets);
        ConfigurableApplicationContext configurableApplicationContext = springApplication.run(args);
        ServerProperties serverProperties = configurableApplicationContext.getBean(ServerProperties.class);
        log.info("--------------------------------------------------------------");
        log.info("服务访问地址为:http://{}:{}{}{}", IpUtils.getHostIP(),
                serverProperties.getPort(),serverProperties.getServlet().getContextPath(),serverProperties.getServlet().getPath());
        log.info("--------------------------------------------------------------");
        log.info("完成启动项目");
    }

}
